<template>
    <el-dialog
        :visible.sync="show"
        :title="type == 62? '提交培养计划' : '提交培养方案'"
        width="58%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="closeWorkFlowDialog"
    >
        <div class="edu-auto-row">
            <div class="edu-auto-col">
                <div class="edu-auto-label">方案名称：</div>
                <div :title="schemeInfo.name" class="edu-auto-content">{{ schemeInfo.name }}</div>
            </div>
            <div class="edu-auto-col">
                <div class="edu-auto-label">英文名称：</div>
                <div :title="schemeInfo.nameEn" class="edu-auto-content">{{ schemeInfo.nameEn }}</div>
            </div>
            <div class="edu-auto-col">
                <div class="edu-auto-label">辅修年级：</div>
                <div
                    :title="schemeInfo.minorGrade"
                    class="edu-auto-content"
                >{{ schemeInfo.minorGrade }}</div>
            </div>
            <div class="edu-auto-col">
                <div class="edu-auto-label">{{$t('minor.major.college')}}：</div>
                <div
                    :title="schemeInfo.collegeCodeI18n"
                    class="edu-auto-content"
                >{{ schemeInfo.collegeCodeI18n }}</div>
            </div>
            <!-- <div class="edu-auto-col">
                <div class="edu-auto-label">进阶层次：</div>
                <div
                    :title="schemeInfo.advancedLevelName"
                    class="edu-auto-content"
                >{{ schemeInfo.advancedLevelName }}</div>
            </div> -->
            <div class="edu-auto-col">
                <div class="edu-auto-label">培养方案模板：</div>
                <div
                    :title="schemeInfo.templateName"
                    class="edu-auto-content"
                >{{ schemeInfo.templateName }}</div>
            </div>
            <div class="edu-auto-col">
                <div class="edu-auto-label">辅修专业：</div>
                <div
                    :title="schemeInfo.majorNameCn"
                    class="edu-auto-content"
                >{{ schemeInfo.majorNameCn }}</div>
            </div>
        </div>
        <div class="hr"></div>
        <div class="edu-auto-row" v-if="!isScheme">
            <div class="edu-auto-col full-col">
                <div class="edu-auto-label require">材料说明：</div>
                <div class="edu-auto-content">
                    <el-input
                        :rows="3"
                        v-model="examineInfo.editExplain"
                        type="textarea"
                        placeholder="请输入内容"
                        style="width: 100%;min-height: 80px;"
                    />
                </div>
            </div>
        </div>
        <div class="edu-auto-row">
            <div class="edu-auto-col" style="width:100%;">
                <div class="edu-auto-label" :class="{'require': !global.isAdmin}" :style="global.isAdmin && {width: '155px'} || {width: '170px'}">签字版培养方案扫描件：</div>
                <div class="edu-auto-content">
                    <el-upload
                        :headers="{'X-Token': $store.state.session.sessionid}"
                        :data="{bucketname: 'face', folderpath: 'file'}"
                        :on-preview="handleAvatarPreview"
                        :on-remove="handleAvatarRemove"
                        :on-success="handleUploadSuccess"
                        :file-list="examineFileList"
                        :before-upload="beforeAvatarUpload"
                        class="upload-demo"
                        ref="uploadFile"
                        action="/api/commonservice/obsfile/uploadfile"
                        multiple
                    >
                        <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
                        <span slot="tip" class="el-upload__tip" style="margin-left:10px">只能上传jpg / png / gif / doc / docx / xlsx / pdf格式的文件, 且单个文件不超过10M</span>
                    </el-upload>
                </div>
            </div>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeWorkFlowDialog" icon="el-icon-close">取 消</el-button>
            <el-button type="primary" @click="submitExamine" icon="el-icon-check">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import {
    checkMinorCulturePlan,
    getCultureSchemeInfoBySchemeId,
    checkCultureScheme
} from "common/src/api/minor/minorCultureScheme";
import mixin from 'common/src/mixins/index';
export default {
    name: "SchemeCheck",
    mixins: [mixin],
    props: {
        show: {
            default: false,
            type: Boolean
        },
        id: {
            default: null,
            type: String
        },
        type: {
            default: null,
            type: String
        },
        randomId: {
            default: 0,
            type: Number
        },
        planId: {
            default: 0,
            type: Number
        }
    },
    data() {
        return {
            schemeInfo: "",
            examineFileList: [],
            examineInfo: {
                type: this.type,
                editExplain: "",
                evidenceList: []
            }
        };
    },
    computed: {
        isScheme() {
            return this.type == 61;
        }
    },
    watch: {
        show(val, oldVal) {
            if (!val) {
                this.examineInfo.editExplain = "";
                this.examineFileList = [];
            }
        }
    },
    methods: {
        initCultureSchemeInfo() {
            getCultureSchemeInfoBySchemeId(this.id).then(res => {
                if (res.code == 200) {
                    this.schemeInfo = res.data;
                }
            });
        },
        handleAvatarRemove(file, fileList) {
            this.examineFileList = fileList;
            this.examineInfo.evidenceList = _.map(fileList, "name");
        },
        handleUploadSuccess(res, file, fileList) {
            this.examineFileList = fileList;
            this.examineInfo.evidenceList = _.map(fileList, item => {
                return item.response.data[item.name].objectkey;
            });
        },
        submitExamine() {
            this.examineInfo.id = this.id;
            let needMsg = true;
            let message = "材料说明和签字版培养方案扫描件不能为空！";
            if (this.examineFileList.length === 0 || (this.examineInfo.editExplain === "" && !this.isScheme)) {
                if (this.global.isAdmin) {
                    this.isScheme && (needMsg = false);
                }
                else {
                    this.isScheme &&(message = "签字版培养方案扫描件不能为空！")
                }
                if (needMsg) {
                    this.$message({
                        message,
                        type: "warning"
                    });
                    return;
                }
            }
            if (this.type == 62) {
                this.examineInfo.id = this.planId;
                checkMinorCulturePlan(this.examineInfo).then(
                    ({ code, data }) => {
                        if (code !== 200) {
                            this.$message({
                                message: res.msg,
                                type: "warning"
                            });
                            return;
                        } else {
                            this.$message({
                                message: "提交成功！",
                                type: "success"
                            });
                            this.examineInfo.editExplain = "";
                            this.examineFileList = [];
                            this.$emit("submitSuccess");
                        }
                    }
                );
            } else {
                checkCultureScheme(this.examineInfo).then(res => {
                    if (res && res.code !== 200) {
                        this.$message({
                            message: res.msg,
                            type: "warning"
                        });
                        return;
                    }
                    this.examineInfo.editExplain = "";
                    this.examineFileList = [];
                    this.$message({
                        message: "提交成功！",
                        type: "success"
                    });
                    this.$emit("returnSchemeList");
                });
            }
        },
        closeWorkFlowDialog() {
            this.$emit("closeWorkFlowDialog");
        },
        beforeAvatarUpload(file) {
            //上传附件的校验
            let requireFile = [
                "jpg",
                "png",
                "gif",
                "doc",
                "docx",
                "xlsx",
                "pdf",
                "JPG",
                "PNG",
                "GIF"
            ];
            const fileFormate =
                requireFile.indexOf(
                    file.name.split(".")[file.name.split(".").length - 1]
                ) > -1;
            const isLt10M = file.size / 1024 / 1024 < 10; //图片大小不超过10MB
            if (!fileFormate) {
                this.$message({
                    message: this.$t("minor.approve.uploadFileError"),
                    type: "warning"
                });
                return false;
            }
            if (!isLt10M) {
                this.$message({
                    message: this.$t("minor.approve.uploadSizeError"),
                    type: "warning"
                });
                return false;
            }
            if (!(fileFormate && isLt10M)) {
                //先清空附件
                return false;
            }
        }
    }
};
</script>
